home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
mint
/
mint110s.zoo
/
procfs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-15
|
18KB
|
744 lines
/*
Copyright 1991,1992 Eric R. Smith.
Copyright 1992,1993,1994 Atari Corporation.
All rights reserved.
*/
/* PROC pseudo-filesystem routines */
/* basically just to allow 'ls -l X:' to give a list of active processes
* some things to note:
* process names are given as name.XXX, where 'XXX' is the pid of the
* process
* process attributes depend on the run queue as follows:
* RUNNING: 0x00 (normal)
* READY: 0x01 (read-only)
* WAIT: 0x20 (archive bit)
* IOBOUND: 0x21 (archive bit+read-only)
* ZOMBIE: 0x22 (archive+hidden)
* TSR: 0x02 (hidden)
* STOP: 0x24 (archive bit+system)
* the general principle is: inactive processes have the archive bit (0x20)
* set, terminated processes have the hidden bit (0x02) set, stopped processes
* have the system bit (0x04) set, and the read-only bit is used to
* otherwise distinguish states (which is unfortunate, since it would be
* nice if this bit corresponded with file permissions).
*/
#include "mint.h"
static long ARGS_ON_STACK proc_root P_((int drv, fcookie *fc));
static long ARGS_ON_STACK proc_lookup P_((fcookie *dir, const char *name, fcookie *fc));
static long ARGS_ON_STACK proc_getxattr P_((fcookie *fc, XATTR *xattr));
static long ARGS_ON_STACK proc_chattr P_((fcookie *fc, int attrib));
static long ARGS_ON_STACK proc_chown P_((fcookie *fc, int uid, int gid));
static long ARGS_ON_STACK proc_chmode P_((fcookie *fc, unsigned mode));
static long ARGS_ON_STACK proc_rmdir P_((fcookie *dir, const char *name));
static long ARGS_ON_STACK proc_remove P_((fcookie *dir, const char *name));
static long ARGS_ON_STACK proc_getname P_((fcookie *root, fcookie *dir, char *pathname,
int size));
static long ARGS_ON_STACK proc_rename P_((fcookie *olddir, char *oldname,
fcookie *newdir, const char *newname));
static long ARGS_ON_STACK proc_opendir P_((DIR *dirh, int flags));
static long ARGS_ON_STACK proc_readdir P_((DIR *dirh, char *nm, int nmlen, fcookie *));
static long ARGS_ON_STACK proc_rewinddir P_((DIR *dirh));
static long ARGS_ON_STACK proc_closedir P_((DIR *dirh));
static long ARGS_ON_STACK proc_pathconf P_((fcookie *dir, int which));
static long ARGS_ON_STACK proc_dfree P_((fcookie *dir, long *buf));
static DEVDRV * ARGS_ON_STACK proc_getdev P_((fcookie *fc, long *devsp));
static long ARGS_ON_STACK proc_open P_((FILEPTR *f));
static long ARGS_ON_STACK proc_write P_((FILEPTR *f, const char *buf, long bytes));
static long ARGS_ON_STACK proc_read P_((FILEPTR *f, char *buf, long bytes));
static long ARGS_ON_STACK proc_lseek P_((FILEPTR *f, long where, int whence));
static long ARGS_ON_STACK proc_ioctl P_((FILEPTR *f, int mode, void *buf));
static long ARGS_ON_STACK proc_datime P_((FILEPTR *f, short *time, int rwflag));
static long ARGS_ON_STACK proc_close P_((FILEPTR *f, int pid));
/* dummy routines from biosfs.c */
extern long ARGS_ON_STACK null_select P_((FILEPTR *f, long p, int mode));
extern void ARGS_ON_STACK null_unselect P_((FILEPTR *f, long p, int mode));
static PROC * name2proc P_((const char *name));
DEVDRV proc_device = {
proc_open, proc_write, proc_read, proc_lseek, proc_ioctl, proc_datime,
proc_close, null_select, null_unselect
};
FILESYS proc_filesys = {
(FILESYS *)0,
0,
proc_root,
proc_lookup, nocreat, proc_getdev, proc_getxattr,
proc_chattr, proc_chown, proc_chmode,
nomkdir, proc_rmdir, proc_remove, proc_getname, proc_rename,
proc_opendir, proc_readdir, proc_rewinddir, proc_closedir,
proc_pathconf, proc_dfree,
nowritelabel, noreadlabel, nosymlink, noreadlink, nohardlink,
nofscntl, nodskchng
};
long ARGS_ON_STACK
proc_root(drv, fc)
int drv;
fcookie *fc;
{
if (drv == PROCDRV) {
fc->fs = &proc_filesys;
fc->dev = drv;
fc->index = 0L;
return 0;
}
fc->fs = 0;
return EINTRN;
}
static PROC *
name2proc(name)
const char *name;
{
const char *pstr;
char c;
int i;
pstr = name;
while ( (c = *name++) != 0) {
if (c == '.')
pstr = name;
}
if (!isdigit(*pstr) && *pstr != '-')
return 0;
i = (int)atol(pstr);
if (i == -1)
return curproc;
else if (i == -2)
i = curproc->ppid;
return pid2proc(i);
}
static long ARGS_ON_STACK
proc_lookup(dir, name, fc)
fcookie *dir;
const char *name;
fcookie *fc;
{
PROC *p;
if (dir->index != 0) {
DEBUG(("proc_lookup: bad directory"));
return EPTHNF;
}
/* special case: an empty name in a directory means that directory */
/* so does "." */
if (!*name || (name[0] == '.' && name[1] == 0)) {
*fc = *dir;
return 0;
}
/* another special case: ".." could be a mount point */
if (!strcmp(name, "..")) {
*fc = *dir;
return EMOUNT;
}
if (0 == (p = name2proc(name))) {
DEBUG(("proc_lookup: name not found"));
return EFILNF;
} else {
fc->index = (long)p;
fc->fs = &proc_filesys;
fc->dev = PROC_RDEV_BASE | p->pid;
}
return 0;
}
static int p_attr[NUM_QUEUES] = { /* attributes corresponding to queues */
0, /* "RUNNING" */
0x01, /* "READY" */
0x20, /* "WAITING" */
0x21, /* "IOBOUND" */
0x22, /* "ZOMBIE" */
0x02, /* "TSR" */
0x24, /* "STOPPED" */
0x21 /* "SELECT" (same as IOBOUND) */
};
static long ARGS_ON_STACK
proc_getxattr(fc, xattr)
fcookie *fc;
XATTR *xattr;
{
PROC *p;
extern int proctime, procdate; /* see dosmem.c */
xattr->blksize = 1;
if (fc->index == 0) {
/* the root directory */
xattr->index = 0;
xattr->dev = xattr->rdev = PROCDRV;
xattr->nlink = 1;
xattr->uid = xattr->gid = 0;
xattr->size = xattr->nblocks = 0;
xattr->mtime = xattr->atime = xattr->ctime = proctime;
xattr->mdate = xattr->adate = xattr->cdate = procdate;
xattr->mode = S_IFDIR | DEFAULT_DIRMODE;
xattr->attr = FA_DIR;
return 0;
}
p = (PROC *)fc->index;
xattr->index = p->pid;
xattr->dev = xattr->rdev = PROC_RDEV_BASE | p->pid;
xattr->nlink = 1;
xattr->uid = p->ruid; xattr->gid = p->rgid;
xattr->size = xattr->nblocks = memused(p);
xattr->mtime = xattr->ctime = xattr->atime = p->starttime;
xattr->mdate = xattr->cdate = xattr->adate = p->startdate;
xattr->mode = S_IMEM | S_IRUSR | S_IWUSR;
xattr->attr = p_attr[p->wait_q];
return 0;
}
static long ARGS_ON_STACK
proc_chattr(fc, attrib)
fcookie *fc;
int attrib;
{
UNUSED(fc); UNUSED(attrib);
return EACCDN;
}
static long ARGS_ON_STACK
proc_chown(fc, uid, gid)
fcookie *fc;
int uid, gid;
{
UNUSED(fc); UNUSED(uid); UNUSED(gid);
return EINVFN;
}
static long ARGS_ON_STACK
proc_chmode(fc, mode)
fcookie *fc;
unsigned mode;
{
UNUSED(fc); UNUSED(mode);
return EINVFN;
}
static long ARGS_ON_STACK
proc_rmdir(dir, name)
fcookie *dir;
const char *name;
{
UNUSED(dir); UNUSED(name);
return EPTHNF;
}
static long ARGS_ON_STACK
proc_remove(dir, name)
fcookie *dir;
const char *name;
{
PROC *p;
if (dir->index != 0)
return EPTHNF;
p = name2proc(name);
if (!p)
return EFILNF;
/* this check is necessary because the Fdelete code checks for
* write permission on the directory, not on individual
* files
*/
if (curproc->euid && curproc->ruid != p->ruid) {
DEBUG(("proc_remove: wrong user"));
return EACCDN;
}
post_sig(p, SIGTERM);
check_sigs(); /* it might have been us */
return 0;
}
static long ARGS_ON_STACK
proc_getname(root, dir, pathname, size)
fcookie *root, *dir; char *pathname;
int size;
{
PROC *p;
UNUSED(root);
/* BUG: we ought to look at size */
UNUSED(size);
if (dir->index == 0)
*pathname = 0;
else {
p = (PROC *)dir->index;
ksprintf(pathname, "%s.03d", p->name, p->pid);
}
return 0;
}
static long ARGS_ON_STACK
proc_rename(olddir, oldname, newdir, newname)
fcookie *olddir;
char *oldname;
fcookie *newdir;
const char *newname;
{
PROC *p;
int i;
if (olddir->index != 0 || newdir->index != 0)
return EPTHNF;
if ((p = name2proc(oldname)) == 0)
return EFILNF;
oldname = p->name;
for (i = 0; i < PNAMSIZ; i++) {
if (*newname == 0 || *newname == '.') {
*oldname = 0; break;
}
*oldname++ = *newname++;
}
retu